<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/31
 * Time: 17:08
 */

use \GatewayWorker\Lib\Db;
require_once __DIR__.'/../Player/Player.php';

class User{
    public $db;
    function __construct()
    {
        $this->db=Db::instance('game');
    }

    /**
     * 将玩家注册进服务器的函数
     * @param $player
     * @return mixed 玩家ID
     */
    function registerUserToDB($player){
         $resId = $this->db->insert('qg_user')->
        cols(array(
            'user_openid' => $player->userOpenId,
            'user_nickname' => $player->userNickname,
            'user_sex' => $player->userSex,
            'user_diamond' => 10,
            'room_card' => 10,
            'user_headimg' => $player->userHeadImg,
            'register_time' => date("Y-m-d H:i:s"),
        ))->query();
        return $resId;
    }

    /**
     * 检查用户是否存在，否存在返回UID[微信登录]
     * @param $playerInfo
     * @return bool
     */
    function judgeUser($playerInfo){
        echo "judgeUser----------";
        print_r($playerInfo);
        $s = $this->db->select('user_uid')->from('qg_user')->where("user_openid=:id")->bindValues(array('id'=>$playerInfo['openId']))->query();

        if($s){
            return $s[0]["user_uid"];
        }else{
            return false;
        }
    }

    /**
     * 检查用户是否存在，否存在返回UID[游客登录]
     * @param $userUid
     * @return bool
     */
    function judgeVisitorsUser($userUid){

        $s = $this->db->select('user_uid')->from('qg_user')->where("user_uid=:id")->bindValues(array('id'=>$userUid))->query();

        if($s){
            return $s[0]["user_uid"];
        }else{
            return false;
        }
    }

    /**
     * 判断用户是否是作弊人员
     * @param $userUid
     * @return mixed
     */
    function judgeRule($userUid){
        $resUser=$this->db->select('user_rule')->from('qg_user')->where("user_uid=:id")->bindValues(array('id'=>$userUid))->query();

        return $resUser[0]["user_rule"];

    }

    /**
     * 获取最新的用户信息
     * @param $userUid
     * @return mixed
     */
    function refreshUser($userUid){
        $resUser=$this->db->select('room_card')->from('qg_user')->where("user_uid=:id")->bindValues(array('id'=>$userUid))->query();
        return $resUser[0];

    }

    /**
     * 扣除房费
     * @param $userUid
     * @param $payNum
     */
    function kouFei($userUid,$payNum){
        $r=$this->db->query("SELECT room_card FROM qg_user WHERE user_uid=$userUid");
        if($r[0]["room_card"]>=$payNum){

            $this->db->query("UPDATE qg_user SET room_card=room_card-$payNum WHERE user_uid=$userUid");
        }
    }
    /**
     * 绑定上级UID
     * @param $userUid
     * @param $superiorUid
     */
    function bindSuperior($userUid,$superiorUid){
        $this->db->query("UPDATE qg_user SET superior_uid=$superiorUid WHERE user_uid=$userUid");
    }

    /**
     * 钻石换房卡(一个钻石换两张房卡)
     * @param $userUid
     * @param $diamond
     * @return 结果 失败时返回false
     */
    function diamondToCards($userUid,$diamond){
        if($diamond<=0){
            return false;
        }

        //查询用户是否有足够多的钻石进行换取
        $resUser=$this->db->query("SELECT user_diamond FROM qg_user WHERE user_uid=$userUid");
        $syDiamond=$resUser[0]["user_diamond"];//剩余钻石数
        if($syDiamond>=$diamond){
            $res=$this->db->query("UPDATE qg_user SET user_diamond=user_diamond-$diamond,room_card=room_card+2*$diamond WHERE user_uid=$userUid");
            if($res){
                $resUser=$this->db->query("SELECT user_diamond as userDiamond,room_card as roomCard FROM qg_user WHERE user_uid=$userUid");
                return $resUser[0];
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 刷新用户的钻石和房卡
     * @param $userUid
     * @return 玩家钻石信息
     */
    function refreshDiamondAndCards($userUid){
        $resUser=$this->db->query("SELECT user_diamond as userDiamond,room_card as roomCard FROM qg_user WHERE user_uid=$userUid");
        if($resUser){
            return $resUser[0];
        }else{
            return false;
        }
    }

    /**
     * 身份证实名认证
     * @param $userUid
     * @param $userName
     * @param $idCard
     * @return bool
     */
    function realNameAuth($userUid,$userName,$idCard){
        $r=$this->db->query("UPDATE qg_user SET user_name='$userName',user_idcard='$idCard',is_certification=2 WHERE user_uid=$userUid");
        if($r){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 查询钻石排行榜
     * @return mixed
     */
    function queryPaiHang(){
        $result=$this->db->query("SELECT user_nickname,user_headimg,user_diamond FROM qg_user ORDER BY user_diamond DESC LIMIT 0,10");
        return $result;
    }

    /**
     * 用户分享
     * @param $userUid
     * @return array(0=>状态(1,当天分享过，但可以继续分享; 2,时间间隔不够，不能分享; 3,分享次数满，不能分享),
     *                1=>当前玩家的分享次数)
     */
    function share($userUid){
        $startTime=mktime(0,0,0,date("m"),date("d"),date("Y"));
        $endTime=mktime(23,59,59,date("m"),date("d"),date("Y"));
        //得到当前用户今天分享的次数
        $t=$this->db->query("SELECT COUNT(*) as share_num FROM qg_share WHERE user_uid=$userUid AND share_time BETWEEN $startTime AND $endTime");

        if($t[0]["share_num"]<3){
            //小于三次 判断用户时间间隔是否为3小时
            //查询当天分享的记录
            $t1=$this->db->query("SELECT * FROM qg_share WHERE user_uid=$userUid AND share_time BETWEEN $startTime AND $endTime ORDER BY share_time DESC");
            if($t1){
                //当天分享了，查询时间
                if(((int)$t1[0]["share_time"])+(3*60*60)<time()){
                    //可以分享
                    $a=time();
                    $this->db->query("INSERT INTO qg_share(user_uid,share_time) VALUES ($userUid,$a)");//插入记录到分享表
                    $this->db->query("UPDATE qg_user SET user_diamond=user_diamond+3 WHERE user_uid=$userUid");//更新钻石数
                    return array(1,$t[0]["share_num"]+1);
                }else{
                    //间隔不够
                    return array(2,$t[0]["share_num"]);
                }

            }else{
                //当天没有分享，可以分享
                $a=time();
                $this->db->query("INSERT INTO qg_share(user_uid,share_time) VALUES ($userUid,$a)");//插入记录到分享表
                $this->db->query("UPDATE qg_user SET user_diamond=user_diamond+1 WHERE user_uid=$userUid");//更新钻石数
                return array(1,$t[0]["share_num"]+1);
            }
        }else{
            //分享次数满了
            return array(3,$t[0]["share_num"]);
        }
    }

    /**
     * 检查用户分享次数的函数
     * @param $userUid 用户ID
     * @return mixed 分享次数
     */
    function checkShare($userUid){
        $startTime=mktime(0,0,0,date("m"),date("d"),date("Y"));
        $endTime=mktime(23,59,59,date("m"),date("d"),date("Y"));
        $t=$this->db->query("SELECT COUNT(*) as share_num FROM qg_share WHERE user_uid=$userUid AND share_time BETWEEN $startTime AND $endTime");//得到当前用户今天分享的次数
        return $t[0]["share_num"];
    }
}